<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $birthday
 * @property integer $sexId
 * @property integer $countryId
 * @property string $lastVisit
 * @property string $dateRegistered
 * @property string $salt
 
 *
 * The followings are the available model relations:
 * @property Character[] $characters
 * @property News[] $news
 * @property Session[] $sessions
 * @property Shoutbox[] $shoutboxes
 * @property Uacoptions[] $uacoptions
 * @property Sex $sex
 * @property Country $country
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('sexId, countryId', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>45),
			array('email', 'length', 'max'=>255),
			array('email', 'email'),
			array('password', 'length', 'max'=>64),
			array('birthday, lastVisit, dateRegistered', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, email, sexId, countryId, lastVisit, dateRegistered', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'characters' => array(self::HAS_MANY, 'Character', 'userId'),
			'news' => array(self::HAS_MANY, 'News', 'userId'),
			'sessions' => array(self::HAS_MANY, 'Session', 'userId'),
			'shoutboxes' => array(self::HAS_MANY, 'Shoutbox', 'userId'),
			'uacoptions' => array(self::MANY_MANY, 'Uacoptions', 'uac(userId, uacoId)'),
			'sex' => array(self::BELONGS_TO, 'Sex', 'sexId'),
			'country' => array(self::BELONGS_TO, 'Country', 'countryId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'email' => 'Email',
			'password' => 'Password',
			'salt' => 'Salt',
			'birthday' => 'Birthday',
			'sexId' => 'Gender',
			'countryId' => 'Country',
			'lastVisit' => 'Last Visit',
			'dateRegistered' => 'Date Registered',
		);
	}
	public function validatePassword($password)
    {
        return $this->hashPassword($password,$this->salt)===$this->password;
    }
 
    public function hashPassword($password,$salt)
    {
        return md5($salt.$password);
    }
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('sexId',$this->sexId);
		$criteria->compare('countryId',$this->countryId);
		$criteria->compare('lastVisit',$this->lastVisit,true);
		$criteria->compare('dateRegistered',$this->dateRegistered,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}